//
//  SinaOriginalView.swift
//  WeiBo01
//
//  Created by mac on 16/1/6.
//  Copyright © 2016年 Han. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class SinaOriginalView: UIView {
    
    //定义一个全局的约束来记录底部约束
    var picConstain: Constraint?
    
    //定义一个属性
    var orginalViewModel: SinaStatusViewModel? {
        
        didSet{
            
            //赋值
            //昵称赋值
            nameLabel.text = orginalViewModel?.model?.user?.screen_name
            //头像
            photoImageView.sd_setImageWithURL(orginalViewModel?.profile_URL)
            //认证头像
            verifitiedImageView.image = orginalViewModel?.verifited_Image
            //等级
            levelImageView.image = orginalViewModel?.vipLevel_Image
            //内容
            contentLabel.text = orginalViewModel?.model?.text
            
            //通过判断配图数量来设置约束
            picConstain?.uninstall()
            
            if orginalViewModel?.model?.pic_urls?.count > 0{
                
                //肯定有配图,然后显示配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    picConstain = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                pictureView.hidden = false
                //传递给配图试图
                pictureView.pictures = orginalViewModel?.model?.pic_urls
                
            }else{
                //没有配图,然后隐藏配图
                self.snp_updateConstraints(closure: { (make) -> Void in
                    picConstain = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
                pictureView.hidden = true
            }
        }
    }

    //1.重写init
    override init(frame: CGRect) {
        super.init(frame: frame)

        //2.添加子控件和约束
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        //添加子控件
        addSubview(photoImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(verifitiedImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //添加约束
        //头像的约束
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top).offset(SinaHomeMargin)
            make.left.equalTo(self.snp_left).offset(SinaHomeMargin)
            make.width.equalTo(40)
            make.height.equalTo(40)
        }
        //昵称约束
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(SinaHomeMargin)
            make.top.equalTo(self.snp_top).offset(SinaHomeMargin)
            
        }
        //等级图片约束
        levelImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(SinaHomeMargin)
            make.centerY.equalTo(nameLabel.snp_centerY)
        }
        //认证图片
        verifitiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(photoImageView.snp_right)
            make.centerY.equalTo(photoImageView.snp_bottom)
        }
        // 时间的约束
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(photoImageView.snp_right).offset(SinaHomeMargin)
            make.top.equalTo(nameLabel.snp_bottom).offset(SinaHomeMargin/2)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(SinaHomeMargin)
            make.centerY.equalTo(timeLabel.snp_centerY)
        }
        //内容约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(SinaHomeMargin)
            make.top.equalTo(photoImageView.snp_bottom).offset(SinaHomeMargin)
            make.width.equalTo(kUIScreenWidth - 2 * CGFloat(SinaHomeMargin))
        }
        
        //给配图添加约束
//        pictureView.backgroundColor = UIColor.blueColor()
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(SinaHomeMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            //固定宽高
//            make.width.equalTo(100)
//            make.height.equalTo(100)
        }
        
        //告诉self的底部是哪里
        self.snp_makeConstraints { (make) -> Void in
           picConstain = make.bottom.equalTo(contentLabel.snp_bottom).constraint
        }
        
        
        
    }
    
    //3.
    //MARK--懒加载
    /// 头像
    lazy var photoImageView: UIImageView = {
        
        let img = UIImageView()
        img.image = UIImage(named: "avatar_default_big")
        return img
    }()
    ///昵称
    lazy var nameLabel: UILabel = {
        
        let label = UILabel()
        
        
        label.text = " 昵称"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.blackColor()
        
//        label.backgroundColor = UIColor.yellowColor()
        
        return label
    }()
    
    ///  等级图片
    lazy var levelImageView: UIImageView = {
        
        
        let img = UIImageView()
        
        img.image = UIImage(named: "common_icon_membership_level1")
        
        return img
        
    }()
    
    ///  认证图片
    lazy var verifitiedImageView: UIImageView = {
        
        let img = UIImageView()
        
        
        img.image = UIImage(named: "avatar_enterprise_vip")
        
        return img
        
    }()
    
    ///   时间
    lazy var timeLabel: UILabel = {
        
        let label = UILabel()
        
        
        label.text = "时间"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.orangeColor()
        
        
        return label
    }()
    
    ///   来源
    lazy var sourceLabel: UILabel = {
        
        let label = UILabel()
        
        
        label.text = "来源"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.darkGrayColor()
        
        
        return label
    }()
    
    
    /// 内容
    lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        
        
        label.text = "内容"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.textColor = UIColor.blackColor()
        
        label.numberOfLines = 0
        
        return label
    }()
    
 /// 配图视图
    lazy var pictureView: SinaPictureView = SinaPictureView()

}
